﻿using AutoMapper;
using MediatR;
using Provenance.Domain.DeepProcess;
using Provenance.ErrorCount;
using Provenance.Infra.Interfaces.DeepProcess;
using Provenance.Infra.Repositories.DeepProcess;
using Provenance.Read.Api.Application.Command.DeepProcess;

namespace Provenance.Read.Api.Application.CommandHandler.DeepProcess
{
    public class BackPackagingMaterialCommandHandler : IRequestHandler<BackPackagingMaterialCommand, ApiResult<PackagingMaterial>>
    {
        private readonly IPackagingMaterialRepository _packagingMaterialRepository;
        private readonly ILogger<BackPackagingMaterialCommandHandler> _logger;
        private readonly IMapper _mapper;
        public BackPackagingMaterialCommandHandler(IPackagingMaterialRepository packagingMaterialRepository, ILogger<BackPackagingMaterialCommandHandler> logger, IMapper mapper)
        {
            _packagingMaterialRepository = packagingMaterialRepository;
            _logger = logger;
            _mapper = mapper;
        }
        public async Task<ApiResult<PackagingMaterial>> Handle(BackPackagingMaterialCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var obj = await _packagingMaterialRepository.GetAsync(x => x.ProductInfoId == request.ProductInfoId);
                return ApiResult<PackagingMaterial>.Success(obj);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return ApiResult<PackagingMaterial>.Failure(ex.Message);
            }
        }
    }
}
